﻿#include "pch.h"
#include "WoJWT.h"
#include "jwtbase.h"

WoJWT WEBOAT_API WoJWTCreate(const CXTextA& issuer,LONGLONG issuedAt,
    LONGLONG expiresAt) {
	return std::make_shared<jwt::builder<jwt::traits::kazuho_picojson>>(
        jwt::builder<jwt::traits::kazuho_picojson>()
        .set_issued_at(
            issuedAt==0? std::chrono::system_clock::now():
            std::chrono::system_clock::time_point(std::chrono::milliseconds(issuedAt))
        )
        .set_expires_at(
            expiresAt==0? 
            std::chrono::system_clock::now() + std::chrono::seconds { 3600 }:
            std::chrono::system_clock::time_point(std::chrono::milliseconds (expiresAt))
        ).set_issuer(issuer.get()));
}

void WoJWTSetIssuer(WoJWT& jwtBuilder, const CXTextA& issuer) {
    jwtBuilder->set_issuer(issuer.get());
    //return jwtBuilder->set_issuer(issuer.get());
}
void WoJWTSetType(WoJWT& jwtBuilder, const CXTextA& type) {
    jwtBuilder->set_type(type.get());
    //return jwtBuilder->set_issuer(issuer.get());
}
void WoJWTSetIssuedAt(WoJWT& jwtBuilder, LONGLONG issuedAt) {
    jwtBuilder->set_issued_at(
        issuedAt == 0 ? std::chrono::system_clock::now() :
        std::chrono::system_clock::time_point(std::chrono::milliseconds(issuedAt))
    );
}

void WoJWTSetExpiresAt(WoJWT& jwtBuilder, LONGLONG expiresAt) {
    jwtBuilder->set_expires_at(
        expiresAt == 0 ?
        std::chrono::system_clock::now() + std::chrono::seconds{ 3600 } :
        std::chrono::system_clock::time_point(std::chrono::milliseconds(expiresAt))
    );
}

void WoJWTAdd(WoJWT& jwtBuilder, const char* id, WoJson& data) {
    picojson::value val;
    picojson::parse(val, data.dump());
    jwtBuilder->set_payload_claim(id, jwt::claim(val));
}

CXTextA WoJWTSignHS256(WoJWT& jwtBuilder,const CXTextA& key) {
    return jwtBuilder->sign(jwt::algorithm::hs256(key.get()));
}
CXTextA WoJWTSignHS384(WoJWT& jwtBuilder, const CXTextA& key) {
    return jwtBuilder->sign(jwt::algorithm::hs384(key.get()));
}
CXTextA WoJWTSignHS512(WoJWT& jwtBuilder, const CXTextA& key) {
    return jwtBuilder->sign(jwt::algorithm::hs512(key.get()));
}
CXTextA WoJWTSignPS256(WoJWT& jwtBuilder,
    const CXTextA& public_key,
    const CXTextA& public_key_password,
    const CXTextA& private_key,
    const CXTextA& private_key_password) {
    return jwtBuilder->sign(jwt::algorithm::ps256(
        public_key.get(),
        private_key.get(),
        public_key_password.get(),
        private_key_password.get()
    ));
}
CXTextA WoJWTSignPS384(WoJWT& jwtBuilder,
    const CXTextA& public_key,
    const CXTextA& public_key_password,
    const CXTextA& private_key,
    const CXTextA& private_key_password) {
    return jwtBuilder->sign(jwt::algorithm::ps384(
        public_key.get(),
        private_key.get(),
        public_key_password.get(),
        private_key_password.get()
    ));
}
CXTextA WoJWTSignPS512(WoJWT& jwtBuilder,
    const CXTextA& public_key,
    const CXTextA& public_key_password,
    const CXTextA& private_key,
    const CXTextA& private_key_password) {
    return jwtBuilder->sign(jwt::algorithm::ps512(
        public_key.get(),
        private_key.get(),
        public_key_password.get(),
        private_key_password.get()
    ));
}
CXTextA WoJWTSignRS256(WoJWT& jwtBuilder,
    const CXTextA& public_key,
    const CXTextA& public_key_password,
    const CXTextA& private_key,
    const CXTextA& private_key_password) {
    return jwtBuilder->sign(jwt::algorithm::rs256(
        public_key.get(),
        private_key.get(),
        public_key_password.get(),
        private_key_password.get()
    ));
}
CXTextA WoJWTSignRS384(WoJWT& jwtBuilder,
    const CXTextA& public_key,
    const CXTextA& public_key_password,
    const CXTextA& private_key,
    const CXTextA& private_key_password) {
    return jwtBuilder->sign(jwt::algorithm::rs384(
        public_key.get(),
        private_key.get(),
        public_key_password.get(),
        private_key_password.get()
    ));
}
CXTextA WoJWTSignRS512(WoJWT& jwtBuilder,
    const CXTextA& public_key,
    const CXTextA& public_key_password,
    const CXTextA& private_key,
    const CXTextA& private_key_password) {
    return jwtBuilder->sign(jwt::algorithm::rs512(
        public_key.get(),
        private_key.get(),
        public_key_password.get(),
        private_key_password.get()
    ));
}

CXTextA WoJWTSignED25519(WoJWT& jwtBuilder,
    const CXTextA& public_key,
    const CXTextA& public_key_password,
    const CXTextA& private_key,
    const CXTextA& private_key_password) {
    return jwtBuilder->sign(jwt::algorithm::ed25519(
        public_key.get(),
        private_key.get(),
        public_key_password.get(),
        private_key_password.get()
    ));
}
CXTextA WoJWTSignED448(WoJWT& jwtBuilder,
    const CXTextA& public_key,
    const CXTextA& public_key_password,
    const CXTextA& private_key,
    const CXTextA& private_key_password) {
    return jwtBuilder->sign(jwt::algorithm::ed448(
        public_key.get(),
        private_key.get(),
        public_key_password.get(),
        private_key_password.get()
    ));
}
CXTextA WoJWTSignES256(WoJWT& jwtBuilder,
    const CXTextA& public_key,
    const CXTextA& public_key_password,
    const CXTextA& private_key,
    const CXTextA& private_key_password) {
    return jwtBuilder->sign(jwt::algorithm::es256(
        public_key.get(),
        private_key.get(),
        public_key_password.get(),
        private_key_password.get()
    ));
}
CXTextA WoJWTSignES256k(WoJWT& jwtBuilder,
    const CXTextA& public_key,
    const CXTextA& public_key_password,
    const CXTextA& private_key,
    const CXTextA& private_key_password) {
    return jwtBuilder->sign(jwt::algorithm::es256k(
        public_key.get(),
        private_key.get(),
        public_key_password.get(),
        private_key_password.get()
    ));
}
CXTextA WoJWTSignES384(WoJWT& jwtBuilder,
    const CXTextA& public_key,
    const CXTextA& public_key_password,
    const CXTextA& private_key,
    const CXTextA& private_key_password) {
    return jwtBuilder->sign(jwt::algorithm::es384(
        public_key.get(),
        private_key.get(),
        public_key_password.get(),
        private_key_password.get()
    ));
}
CXTextA WoJWTSignES512(WoJWT& jwtBuilder,
    const CXTextA& public_key,
    const CXTextA& public_key_password,
    const CXTextA& private_key,
    const CXTextA& private_key_password) {
    return jwtBuilder->sign(jwt::algorithm::es512(
        public_key.get(),
        private_key.get(),
        public_key_password.get(),
        private_key_password.get()
    ));
}